package com.ruoyi.fitsculpt.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fitsculpt.domain.EasyfitUserProfiles;
import com.ruoyi.fitsculpt.service.IEasyfitUserProfilesService;
import com.ruoyi.fitsculpt.service.IEasyfitUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户详细信息Controller
 *
 * @author metaee
 * @date 2025-02-27
 */
@Api(tags = "用户详细信息管理")
@RestController
@RequestMapping("/fitsculpt/profiles")
public class EasyfitUserProfilesController extends BaseController {
    @Autowired
    private IEasyfitUserProfilesService easyfitUserProfilesService;

    @Autowired
    private IEasyfitUsersService easyfitUsersService;

    /**
     * 查询用户详细信息列表
     */
    @ApiOperation("查询用户详细信息列表")
    @GetMapping("/list")
    public TableDataInfo list(EasyfitUserProfiles easyfitUserProfiles) {
        startPage();
        List<EasyfitUserProfiles> list = easyfitUserProfilesService.selectEasyfitUserProfilesList(easyfitUserProfiles);
        return getDataTable(list);
    }

    /**
     * 导出用户详细信息列表
     */
    @ApiOperation("导出用户详细信息列表")
    @Log(title = "用户详细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasyfitUserProfiles easyfitUserProfiles) {
        List<EasyfitUserProfiles> list = easyfitUserProfilesService.selectEasyfitUserProfilesList(easyfitUserProfiles);
        ExcelUtil<EasyfitUserProfiles> util = new ExcelUtil<EasyfitUserProfiles>(EasyfitUserProfiles.class);
        util.exportExcel(response, list, "用户详细信息数据");
    }

    /**
     * 获取用户详细信息详细信息
     */
    @ApiOperation("获取用户详细信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        EasyfitUserProfiles profile = easyfitUserProfilesService.selectEasyfitUserProfilesById(id);
        if (profile != null && profile.getUserId() != null) {
            // 关联查询用户基本信息
            EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(profile.getUserId());
            profile.setUser(user);

            // 计算BMI
            if (profile.getHeight() != null && profile.getWeight() != null) {
                // BMI = 体重(kg) / (身高(m))²
                BigDecimal heightInMeters = profile.getHeight().divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
                BigDecimal bmi = profile.getWeight().divide(heightInMeters.multiply(heightInMeters), 1, RoundingMode.HALF_UP);
                profile.setBmi(bmi);
            }

            // 计算基础代谢率(BMR)
            if (profile.getWeight() != null && profile.getHeight() != null && profile.getAge() != null && profile.getGender() != null) {
                BigDecimal bmr;
                if ("1".equals(profile.getGender())) { // 男性
                    // BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄 + 5
                    bmr = new BigDecimal("10").multiply(profile.getWeight())
                        .add(new BigDecimal("6.25").multiply(profile.getHeight()))
                        .subtract(new BigDecimal("5").multiply(new BigDecimal(profile.getAge())))
                        .add(new BigDecimal("5"));
                } else { // 女性
                    // BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄 - 161
                    bmr = new BigDecimal("10").multiply(profile.getWeight())
                        .add(new BigDecimal("6.25").multiply(profile.getHeight()))
                        .subtract(new BigDecimal("5").multiply(new BigDecimal(profile.getAge())))
                        .subtract(new BigDecimal("161"));
                }
                profile.setBasalMetabolicRate(bmr.setScale(0, RoundingMode.HALF_UP));
            }

            // 计算每日所需热量
            if (profile.getBasalMetabolicRate() != null && profile.getActivityLevel() != null) {
                BigDecimal activityMultiplier;
                switch (profile.getActivityLevel().intValue()) {
                    case 1: // 久坐不动
                        activityMultiplier = new BigDecimal("1.2");
                        break;
                    case 2: // 轻度活动
                        activityMultiplier = new BigDecimal("1.375");
                        break;
                    case 3: // 中度活动
                        activityMultiplier = new BigDecimal("1.55");
                        break;
                    case 4: // 重度活动
                        activityMultiplier = new BigDecimal("1.725");
                        break;
                    case 5: // 非常活跃
                        activityMultiplier = new BigDecimal("1.9");
                        break;
                    default:
                        activityMultiplier = new BigDecimal("1.2");
                }
                BigDecimal dailyCalories = profile.getBasalMetabolicRate().multiply(activityMultiplier);
                profile.setDailyCaloriesNeeded(dailyCalories.setScale(0, RoundingMode.HALF_UP));
            }
        }
        return success(profile);
    }

    /**
     * 新增用户详细信息
     */
    @ApiOperation("新增用户详细信息")
    @Log(title = "用户详细信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EasyfitUserProfiles easyfitUserProfiles) {
        return toAjax(easyfitUserProfilesService.insertEasyfitUserProfiles(easyfitUserProfiles));
    }

    /**
     * 修改用户详细信息
     */
    @ApiOperation("修改用户详细信息")
    @Log(title = "用户详细信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EasyfitUserProfiles easyfitUserProfiles) {
        return toAjax(easyfitUserProfilesService.updateEasyfitUserProfiles(easyfitUserProfiles));
    }

    /**
     * 删除用户详细信息
     */
    @ApiOperation("删除用户详细信息")
    @Log(title = "用户详细信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{profileIds}")
    public AjaxResult remove(@ApiParam(value = "用户详细信息ID数组", required = true) @PathVariable Long[] profileIds) {
        return toAjax(easyfitUserProfilesService.deleteEasyfitUserProfilesByProfileIds(profileIds));
    }
}
